from rest_framework import serializers
from django.contrib.auth.models import Permission, ContentType,Group
from users.models import User


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = '__all__'
        extra_kwargs={
            'write_only':True
        }

    def create(self, validated_data):
        # 添加字段
        validated_data['is_staff']=True
        admin=super().create(validated_data)

        # 加密密码
        password=validated_data['password']
        admin.set_password(password)
        admin.save()

        return admin

    def update(self, instance, validated_data):
        # 添加字段

        admin = super().update(instance,validated_data)

        # 加密密码
        password = validated_data['password']
        admin.set_password(password)
        admin.save()

        return admin


class GroupSerializer(serializers.ModelSerializer):
    class Meta:
        model = Group
        fields = '__all__'


